package A1_二叉树;

public class LC_450删除二叉搜索树中的节点 {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val, TreeNode left, TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }
    public TreeNode deleteNode(TreeNode root, int key) {

        if(root==null)
            return null;
        if(root.val==key){
            if(root.left==null && root.right==null)
                return null;
            else if(root.left!=null && root.right==null)
                return root.left;
            else if(root.left==null && root.right!=null)
                return root.right;
            else{
                TreeNode cur=root.right;
                while(cur.left!=null)
                    cur=cur.left;
                cur.left=root.left;
                return root.right;
            }
        }
        if(key<root.val)
            root.left=deleteNode(root.left,key);
        if(key>root.val)
            root.right=deleteNode(root.right,key);
        return root;
    }
}
